﻿using BySoft.Server.Common;
using BySoft.Server.Common.Entity;
using BySoft.Server.DataAccess;
using BySoft.Server.DataAccess.UserCode;
using OneSoft.DataAccess;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace BySoft.Server.Business.UserCode
{
    public partial class BsMatUseBusiness
    {
        public static void SaveOrUpdate(BsMatuseEntity Entity, DataTable DtMatUseData, string strType)
        {
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                broker.BeginTransaction();
                try
                {
                    if (strType == "Save")
                    {
                        DataAccessCommon.Insert(Entity, broker);
                    }
                    else
                    {
                        DataAccessCommon.UpdateOnly(Entity, broker);
                    }
                    if (DtMatUseData != null && DtMatUseData.Rows.Count > 0)
                    {
                        foreach (DataRow drItem in DtMatUseData.Rows)
                        {
                            BsMatusedetailEntity en = new BsMatusedetailEntity();
                            en.BillID = Entity.BillID;
                            en.ScheduleMatID = CommonMethod.ConvertToDecimal(drItem["ScheduleMatID"]);
                            en.MaterialID = CommonMethod.GetString(drItem["MaterialID"]);
                            en.Nums = CommonMethod.ConvertToDecimal(drItem["Nums"]);
                            en.MMNumber = CommonMethod.ConvertToDecimal(drItem["MMNumber"]);
                            en.Remark = CommonMethod.GetString(drItem["Remark"]);

                            string strIsNew = CommonMethod.GetString(drItem["IsNew"]);
                            string strIsDel = CommonMethod.GetString(drItem["IsDel"]);
                            if (strIsNew == "1" && strIsDel != "1")
                            {
                                DataAccessCommon.Insert(en, true, broker);
                            }
                            if (strIsNew != "1" && strIsDel != "1")
                            {
                                en.BillDetailID = CommonMethod.ConvertToInt(drItem["BillDetailID"]);
                                DataAccessCommon.UpdateOnly(en, broker);
                            }
                            if (strIsNew != "1" && strIsDel == "1")
                            {
                                //删除明细
                                ClsCommDA.Del("BS_MatUseDetail", "BillDetailID", CommonMethod.GetString(drItem["BillDetailID"]), broker);
                            }
                        }
                    }
                    broker.Commit();
                }
                catch
                {
                    broker.Rollback();
                    throw;
                }
            }
        }

        public static DataTable SelectDialogScheduleDetailList(string strWorkOrderNo, string strScheduleID, string strProductID, string strProductName, int pageIndex, int PageSize, out int m_count)
        {
            return BsMatUseDataAccess.SelectDialogScheduleDetailList(strWorkOrderNo,strScheduleID,strProductID,strProductName,pageIndex,PageSize,out m_count);
        }

        public static DataTable SelectMaterial(string strMaterialID, string strMaterialName, string ScheduleDetailID, string BillID, int pageIndex, int PageSize, out int m_count)
        {
            return BsMatUseDataAccess.SelectMaterial(strMaterialID, strMaterialName, ScheduleDetailID, BillID, pageIndex, PageSize, out m_count);
        }

        public static void Del(string strCode)
        {
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    broker.BeginTransaction();
                    ClsCommDA.Del("BS_MatUseDetail", "BillID", strCode, broker);
                    ClsCommDA.Del("BS_MatUse", "BillID", strCode, broker);
                    broker.Commit();
                }
                catch
                {
                    broker.Rollback();
                    throw;
                }
            }
        }

        public static DataTable SelectList(string strBillID, string strFrom, string strTo, string strWorkOrderNo, string MaterialName, int IMatUseCount, out int icurPageIndex, int pageIndex, int PageSize, out int m_count)
        {
            return BsMatUseDataAccess.SelectList(strBillID, strFrom, strTo, strWorkOrderNo, MaterialName, IMatUseCount, out icurPageIndex, pageIndex, PageSize, out m_count);
        }

        public static DataTable GetDataByID(string id)
        {
            return BsMatUseDataAccess.GetDataByID(id);
        }
    }
}
